package com.activity;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

public class OnePixelActivity extends Activity {

    private BroadcastReceiver endReceiver = null;
    private static  String myTag = "OnePixelActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Log.w(myTag,"OnePixelActivity onCreate");
        super.onCreate(savedInstanceState);
        //设置1像素
        Window window = getWindow();
        window.setGravity(Gravity.LEFT | Gravity.TOP);
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = 0;
        params.y = 0;
        params.height = 50;
        params.width = 50;
        window.setAttributes(params);
        if(endReceiver == null){
            //结束该页面的广播
            endReceiver = new BroadcastReceiver()
            {
                @Override
                public void onReceive(Context context, Intent intent)
                {
                    finish();
                    Log.w(myTag,"endReceiver 结束1像素的Activity");
                }
            };
        }
        registerReceiver(endReceiver, new IntentFilter("finish"));
        //检查屏幕状态
        checkScreen();
    }
    @Override
    protected void onResume()
    {
        Log.w(myTag,"OnePixelActivity onResume");
        checkScreen();
        super.onResume();
    }
    /** * 检查屏幕状态 isScreenOn为true 屏幕“亮”结束该Activity */
    private void checkScreen()
    {
        PowerManager pm = (PowerManager) OnePixelActivity.this.getSystemService(Context.POWER_SERVICE);
        boolean isScreenOn = pm.isScreenOn();
        if (isScreenOn)
        {
            finish();
        }
    }

    @Override
    protected void onPause() {
        Log.w(myTag,"OnePixelActivity onPause");
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        Log.w(myTag,"OnePixelActivity onDestroy");
        if(endReceiver != null){
            unregisterReceiver(endReceiver);
            endReceiver  = null;
        }
        super.onDestroy();
    }
}
